/**
 * @file CListBox.cpp
 * @author SoBigDog (623644973lubin@163.com)
 * @brief 
 * @version 0.1
 * @date 2020-04-10
 * 
 * @copyright Copyright (c) 2020
 * 
 */
#include "ListBox.h"

namespace {
	const std::string str_id ("CAN id"); 
	const std::string str_ch ("CH");
	const std::string str_dlc("DLC");
	const std::string str_dir("DIR");
	const std::string str_tim("Timestamp");
	const std::string str_dat("data");
	const std::string str_rat("rate");
};

ListBox::ListBox(WINDOW * parent, int len_y, int len_x, int y, int x) : Widget(parent, len_y, len_x, y, x)
{
	std::vector<std::string> head;// = new std::vector<std::string>;
	head.push_back(str_id);
	head.push_back(str_ch);
	head.push_back(str_dlc);
	head.push_back(str_dir);
	head.push_back(str_tim);
	head.push_back(str_dat);
	head.push_back(str_rat);

	//m_list_string_pos.push_back(m_list[j][i].length() + (4 - m_list[j][i].length() % 4));
	m_list_string_pos.push_back(12);
	m_list_string_pos.push_back(4);
	m_list_string_pos.push_back(4);
	m_list_string_pos.push_back(4);
	m_list_string_pos.push_back(12);
	m_list_string_pos.push_back(40);
	m_list_string_pos.push_back(4);

	m_list.push_back(head);
	fresh();	
}

ListBox::~ListBox()
{

}

int ListBox::fresh(void)
{
	wclear(win);
	int temp_pos;
	for (int j = 0; j < (int)m_list.size(); ++j) {
		temp_pos = 0;
		for (int i = 0; i < 7; ++i) {
			mvwprintw(win, j, temp_pos, "%s", m_list[j][i].c_str());
			temp_pos += m_list_string_pos[i];
		}
	}

	wrefresh(win);
	return 0;
}

/**
 * @brief 
 * 
 * @param y 
 * @param x 
 * @param str 
 * @return int 
 */
int ListBox::push(std::vector<std::string> item)
{
	bool new_item = true;

	if ((int)item.size() != 7) {
		return -1;
	}

	for (int i = 0; i < (int)m_list.size(); ++i) {
		if (m_list[i][0] == item[0]) {
			new_item = false;
			for (int j = 0; j < 7; ++j) {
				m_list[i][j] = item[j];
			}
		}
	}

	if (new_item)
		m_list.push_back(item);

	return 0;
}
